﻿var main_MeasureDistance = function (point1, point2) {
    var dlat = (point2.y - point1.y);
    var dlon = (point2.x - point1.x);

    var A = 111100 * dlat;
    var B = 111100 * dlon * Math.cos(getAngle((point2.y + point1.y) / 2));
    var C = Math.pow(A, 2) + Math.pow(B, 2);
    return Math.sqrt(C);
};

var getAngle = function (rad) {
    return rad * Math.PI / 180;
};

//经纬度坐标转换墨卡托投影坐标
// var lonlat2Mercator = function (lonlat) {
//     if (lonlat.x < -180 || lonlat.x > 180 || lonlat.y > 90 || lonlat.y < -90) {
//         return false;
//     }
//
//     var point = null;
//     require(["esri/geometry/Point", "esri/SpatialReference"], function (Point, SpatialReference) {
//         var x = lonlat.x * 20037508.34 / 180;
//         var y = Math.log(Math.tan((90 + lonlat.y) * Math.PI / 360)) / (Math.PI / 180);
//         y = y * 20037508.34 / 180;
//         point = new Point(x, y, new SpatialReference({wkid: 102113}));
//     });
//     return point;
// };

var mercator2lonlat = function (mercator) {
    var x = mercator.x / 20037508.34 * 180;
    var y = mercator.y / 20037508.34 * 180;
    y = 180 / Math.PI * (2 * Math.atan(Math.exp(y * Math.PI / 180)) - Math.PI / 2);

    if (x < -180 || x > 180 || y > 90 || y < -90) {
        return false;
    }
    return new esri.geometry.Point(x, y);
};

function lonlat2Mercator(lonlat) {
    if (lonlat.x < -180 || lonlat.x > 180 || lonlat.y > 90 || lonlat.y < -90) {
        return false;
    }
    var x = lonlat.x * 20037508.34 / 180;
    var y = Math.log(Math.tan((90 + lonlat.y) * Math.PI / 360)) / (Math.PI / 180);
    y = y * 20037508.34 / 180;
    return new esri.geometry.Point(x, y, new esri.SpatialReference({wkid: 102113}));
}

var formatPoint = function (lon, lat) {
    var x = lon / 10000000.0;
    var y = lat / 10000000.0;
    return new esri.geometry.Point(x, y, new esri.SpatialReference({kid: 4326}));
};

//将checkbox中读出的value进行位操作
var getCheckVal = function (chk_value) {
    var a = 0;
    for (var i = 0; i < chk_value.length; i++) {
        if (chk_value[i] === '0') {
            a = a | 1;
        }
        if (chk_value[i] === '1') {
            a = a | 2;
        }
        if (chk_value[i] === '2') {
            a = a | 4;
        }
        if (chk_value[i] === '3') {
            a = a | 8;
        }
        if (chk_value[i] === '4') {
            a = a | 16;
        }
    }
    return a;
};

//将端口中读出的value进行解析
var readCheckVal = function () {

};

var formatGeometryPoint = function (geometryPoint) {
    return new esri.geometry.Point(geometryPoint[0], geometryPoint[1]);
};
var springMsg = function (title, content) {
    $.messager.show({
        title: title,
        msg: content,
        timeout: 3000,
        showType: 'slide'
    });
};

function Dictionary() {
    this.data = [];

    this.put = function (key, value) {
        this.data[key] = value;
    };

    this.get = function (key) {
        return this.data[key];
    };

    this.remove = function (key) {
        this.data[key] = null;
    };

    this.isEmpty = function () {
        if (this.data.length === 0) {
            return true;
        } else {
            return false;
        }
    };

    this.size = function () {
        return this.data.length;
    };

    this.containKey = function (key) {
        if (this.data[key]) {
            return true;
        } else {
            return false;
        }
    }
}

function insert_flg(str, flg, sn) {
    var newstr = "";
    for (var i = 0; i < str.length; i += sn) {
        var tmp = str.substring(i, i + sn);
        newstr += tmp + flg;
    }
    return newstr;
}

var Degree = function (formatStyle, value, str) {
    return formatStyle.formatDegree(value, str);
};

var MarkerDegree = function () {

};
MarkerDegree.prototype.formatDegree = function (value, str) {
    var degree = Math.abs(value);
    if (degree >= 360) {
        degree = degree - 360 * (Math.floor(degree / 360));//Math.floor取整，Math.abs绝对值
    }
    var v1 = Math.floor(degree);//度
    var v2 = ((degree - v1) * 60).toFixed(3);//分

    switch (str) {
        case "lon":
            return {degree: (value >= 0 ? "E " : "W " ) + v1, minute: v2};
            break;
        case "lat":
            return {degree: (value >= 0 ? "N " : "S " ) + v1, minute: v2};
            break;
    }
};

var ConsoleXY = function () {

};
ConsoleXY.prototype.formatDegree = function (value, str) {
    var degree = Math.abs(value);
    if (degree >= 360) {
        degree = degree - 360 * (Math.floor(degree / 360));//Math.floor取整，Math.abs绝对值
    }

    var v1 = Math.floor(degree);//度
    var v2 = Math.floor((degree - v1) * 60);//分
    var v3 = Math.round((degree - v1) * 3600 % 60);//秒

    switch (str) {
        case "lon":
            return (value >= 0 ? "E " : "W " ) + v1 + '°' + v2 + '\'' + v3 + '"';
            break;
        case "lat":
            return (value >= 0 ? "N " : "S " ) + v1 + '°' + v2 + '\'' + v3 + '"';
            break;
    }
};

function updateMarkerList(obj) {
    return user.markers.push(obj), localStorage.markers = JSON.stringify(user.markers);
}

function buildMarkerList(data) {
    $(".markerlist").empty();

    for (var i = 0, l = data.length; i < l; i++) {
        var li = "<li id='" + data[i].name + "'><span>" + data[i].name + "</span><a class='uk-icon-close .uk-icon-medium'></a></li>";
        $(".markerlist").append($(li));
    }
}

function consoleTime() {
    var time = new Date();
    // 程序计时的月从0开始取值后+1
    var m = time.getMonth() + 1;
    var t = time.getFullYear() + "-" + m + "-"
        + time.getDate() + " " + time.getHours() + ":"
        + time.getMinutes() + ":" + time.getSeconds();
    console.log(t);
}

function getLocalTime() {
    var time = new Date();
    var t = time.getFullYear(), m = time.getMonth() + 1;
    return {"localYear": t, "localMonth": m};
}

function getLocalDays(month, year) {
    if (month === 2) {
        return year % 4 === 0 ? 29 : 28;
    } else if (month === 1 || month === 3 || month === 5 || month === 7 || month === 8 || month === 10 || month === 12) {
        //月份为：1,3,5,7,8,10,12 时，为大月.则天数为31；
        return 31;
    } else {
        //其他月份，天数为：30.
        return 30;
    }
}

var formatTime = function (dtDataTime) {
    function date(originTime) {
        var year = originTime.getFullYear(), month = originTime.getMonth() + 1, day = originTime.getDate(),
            hour = originTime.getHours(), minutes = originTime.getMinutes(), seconds = originTime.getSeconds();
        return year + "-" + month + "-" + day + "&nbsp;" + hour + ":" + minutes + ":" + seconds;
    }

    var originTime = new Date(dtDataTime.NYear, dtDataTime.NMonth, dtDataTime.NDay, dtDataTime.NHour, dtDataTime.NMin, dtDataTime.NSec);
    return date(originTime);
};


function formatTime_datagrid(dtDataTime) {
    function date(originTime) {
        var year = originTime.getFullYear(), month = originTime.getMonth(), day = originTime.getDate(),
            hour = originTime.getHours(), minutes = originTime.getMinutes(), seconds = originTime.getSeconds();
        return year + "-" + month + "-" + day + "&nbsp;" + hour + ":" + minutes + ":" + seconds;
    }

    var originTime = new Date(dtDataTime.NYear, dtDataTime.NMonth, dtDataTime.NDay, dtDataTime.NHour, dtDataTime.NMin, dtDataTime.NSec);
    return date(originTime);
}

var formatWeatherTime = function (dtDataTime) {
    function date(originTime) {
        var hour = originTime.getHours(), minutes = originTime.getMinutes();
        return minutes.toString().length === 1 ? hour + ":0" + minutes : hour + ":" + minutes;
    }

    var originTime = new Date(dtDataTime.NYear, dtDataTime.NMonth, dtDataTime.NDay, dtDataTime.NHour, dtDataTime.NMin, dtDataTime.NSec);
    return date(originTime);
};

function formatDate(dtDataTime) {
    var time = new Date(dtDataTime.NYear, dtDataTime.NMonth, dtDataTime.NDay, dtDataTime.NHour, dtDataTime.NMin, dtDataTime.NSec);
    return time.toDateString();
}

var setTime = function (year, month, day, hour, min, second) {
    return {
        NYear: year,
        NMonth: month,
        NDay: day,
        NHour: hour,
        NMin: min,
        NSec: second
    }
};

function stringToDate(str) {
    if (str === "") return console.log("时间未选择");
    var strArray = str.split("T"), strDate = strArray[0].split("-"), strTime = strArray[1].split(":");
    // var a = new Date(strDate[0], strDate[1], strDate[2], strTime[0], strTime[1], "0");
    return setTime(parseInt(strDate[0]), parseInt(strDate[1]), parseInt(strDate[2]), parseInt(strTime[0]), parseInt(strTime[1]), 0);
}

var getCss = function (o, key) {
    // getComputedStyle是为了兼容FF浏览器
    return o.currentStyle ? o.currentStyle[key] : document.defaultView.getComputedStyle(o, false)[key];
};

// var computTime = function (startTime, endTime) {
//     var startDate = stringToDate(startTime), endDate = stringToDate(endTime);
//     // var start = getDays(startDate.NYear) * startDate.NYear +;
//
//     function getDays(year) {
//         return year % 4 === 0 ? 366 : 365;
//     }
// };

function formatSystemDate(str) {
    if (str === "") return console.log("时间未选择");
    var strArray = str.split("T"), strDate = strArray[0].split("-"), strTime = strArray[1].split(":");
    return new Date(parseInt(strDate[0]), parseInt(strDate[1]) - 1, parseInt(strDate[2]), parseInt(strTime[0]), parseInt(strTime[1]), 0)
}

function computeTime(startTime, endTime) {
    var finalDate = formatSystemDate(endTime), startDate = formatSystemDate(startTime);   // 获取当前时间
    // 计算时间差
    return finalDate - startDate;
}

function presentTime(backInterval) {
    var now = new Date();
    var str = now.getFullYear() + "-" + fix((now.getMonth() + 1), 2) + "-" + fix(now.getDate(), 2) + "T" + fix(now.getHours(), 2) + ":" + fix(now.getMinutes(), 2);
    var t_s = now.getTime();
    now.setTime(t_s - 1000 * 60 * backInterval);
    var backStr = now.getFullYear() + "-" + fix((now.getMonth() + 1), 2) + "-" + fix(now.getDate(), 2) + "T" + fix(now.getHours(), 2) + ":" + fix(now.getMinutes(), 2);
    return {
        "now": str,
        "back": backStr
    };
}

function fix(num, length) {
    return ('' + num).length < length ? ((new Array(length + 1)).join('0') + num).slice(-length) : '' + num;
}